package edu.usc.epigenome.dnaase.script;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

public class vcfConcordanceWithSoapGeno {

	final private static String USAGE = "vcfConcordanceWithSoapGeno [opts] vcfFileName soapFileName ...";
	
	/**
	 * @param args
	 */
	@Option(name="-pValue",usage="p value thersh hold for select heterozygous snp sites in soapsnp result")
	protected double pValue = 0.05;
	@Option(name="-samtools",usage="samtools vcf format")
	protected boolean samtools = false;
	
	@Argument
	private List<String> stringArgs = new ArrayList<String>();
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new vcfConcordanceWithSoapGeno().doMain(args);

	}

	public void doMain(String[] args) throws IOException{
		CmdLineParser parser = new CmdLineParser(this);
		// if you have a wider console, you could increase the value;
		// here 80 is also the default
		parser.setUsageWidth(80);
		try
		{
			parser.parseArgument(args);
			if (stringArgs.size() < 2) throw new CmdLineException(USAGE);
		
		}
		catch (CmdLineException e)
		{
			System.err.println(e.getMessage());
			// print the list of available options
			parser.printUsage(System.err);
			System.err.println();
			return;
		}

		String vcfFileName = stringArgs.remove(0);
		String soapFileName = stringArgs.remove(0);
		HashMap<String,Double> vcf1map = new HashMap<String,Double>();
		BufferedReader br1 = new BufferedReader(new FileReader(vcfFileName));
		int hetConcordance = 0;
		int soapHet_bsHom = 0;
		int bsHet_soapHom = 0;
		int soapHet = 0;
		int bsHet = 0;
		int soapTotal = 0;
		int bsTotal = 0;
		
		String line;
		while( (line = br1.readLine()) != null){
			if(line.startsWith("#"))
				continue;
			String[] tmpArray = line.split("\t");
			String key = tmpArray[0] + "-" + tmpArray[1];
			String[] attributes = tmpArray[7].split(";");
			double word = -1;
			for(String attribute : attributes){
				if(samtools){
					if(attribute.matches("AF1=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word = Double.parseDouble(tempAF[0]);
					}
				}
				else{
					if(attribute.matches("AF=\\S+")){
						String[] temp = attribute.split("=");
						String[] tempAF = temp[1].split(",");
						word = Double.parseDouble(tempAF[0]);
					}
				}
				
			}
			if(word != -1){
				vcf1map.put(key, word);
				if(word != 0 && word != 1.0)
					bsHet++;
			}
			bsTotal++;
		}
		br1.close();
		BufferedReader br2 = new BufferedReader(new FileReader(soapFileName));
		while( (line = br2.readLine()) != null){
			
			String[] tmpArray = line.split("\t");
			if(tmpArray[2].equalsIgnoreCase("N") && tmpArray[3].equalsIgnoreCase("N"))
				continue;
			String key = tmpArray[0] + "-" + tmpArray[1];
			//String ref = tmpArray[2];
			//String consensus = tmpArray[3];
			double pRanksum = Double.parseDouble(tmpArray[14]);
			if(pRanksum < pValue)
				soapHet++;
			if(vcf1map.containsKey(key)){
				
				double value = vcf1map.get(key);
				if((value != 0 && value != 1.0) && pRanksum < pValue){
					hetConcordance++;
				}
				else if((value != 0 && value != 1.0) && pRanksum >= pValue){
					bsHet_soapHom++;
					System.out.println(key);
				}
				else if((value == 0 || value == 1.0) && pRanksum < pValue){
					soapHet_bsHom++;
					System.out.println(key);
				}
				else{
					//System.err.println(key);
				}
				
			}
			else{
				if(pRanksum < pValue){
					soapHet_bsHom++;
					System.out.println(key);
				}
					
			}
			soapTotal++;
			
		}
		br2.close();
		System.out.println("bsTotal: " + bsTotal);
		System.out.println("bsHet: " + bsHet);
		System.out.println("soapTotal: " + soapTotal);
		System.out.println("soapHet: " + soapHet);
		System.out.println("hetConcordance: " + hetConcordance);
		System.out.println("bsHet_soapHom: " + bsHet_soapHom);
		System.out.println("soapHet_bsHom: " + soapHet_bsHom);
	}
	
}

